package com.study.d2_dom4j_xml;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

public class Test2 {
    public static void main(String[] args) throws Exception{
        //做案例，封装xml中对象到ArrayList
        List<User> list = new ArrayList<>();
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read("day11/src/helloworld.xml");
        Element rootElement = doc.getRootElement();
        List<Element> elements = rootElement.elements("user");
        for (Element element : elements) {
            list.add(new User(element.elementTextTrim("name"),
                    element.elementText("sex").charAt(0),
                    Integer.valueOf(element.elementTextTrim("age")),
                    element.elementTextTrim("hobby")));
        }
        for (User user : list) {
            System.out.println(user);
        }

    }
}
